//
//  RichAreaViewController.m
//  Consume100
//
//  Created by eliot on 16/3/25.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "RichAreaViewController.h"
#import "ChangePurchaeseTableViewCell.h"
#import "GoodsDetailViewController.h"
@interface RichAreaViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *topView;

@property (nonatomic, strong) UITableView *tableView;

/** 全部*/
@property (nonatomic, strong) UITableView *allTableView;
/** 手机数码*/
@property (nonatomic, strong) UITableView *digitalTableView;
/** 全部数据源*/
@property (nonatomic, strong) NSArray *allArr;

/** 手机数码数据源*/
@property (nonatomic, strong) NSArray *digitalelArr;

/** 背景视图*/
@property (nonatomic, strong) UIView *blackView;


@property (nonatomic, strong) UILabel *lb1;

@property (nonatomic, strong) UILabel *lb2;

@end

@implementation RichAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"兑换商城";
    self.tableView.hidden = NO;
    self.topView.hidden = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ChangePurchaeseTableViewCell" bundle:nil] forCellReuseIdentifier:@"ChangePurchaeseTableViewCell"];
    
}


-(UIView *)blackView {
    if (!_blackView) {
        //灰色背景
        _blackView = [[UIView alloc] init];
        [self.view addSubview:_blackView];
        _blackView.frame = CGRectMake(0, 50, self.view.frame.size.width, 0);
        _blackView .backgroundColor = [UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:0.6];
        
        
         _allTableView = [[UITableView alloc] init];
        _allTableView.delegate = self;
        _allTableView.dataSource = self;
        [self.view addSubview:_allTableView];
        _allTableView.frame = CGRectMake(0, 50, Width, 0);
        
        
        _digitalTableView = [[UITableView alloc] init];
        _digitalTableView.delegate = self;
        _digitalTableView.dataSource = self;
        [self.view addSubview:_digitalTableView];
        _allTableView.frame = CGRectMake(0, 50, Width, 0);
        
        
    }
    return _blackView;
}

-(NSArray *)allArr {
    if (!_allArr) {
        _allArr = @[@"全部",
                       @"0元专区",
                       @"10币专区",
                       @"百币专区",
                       @"土豪专区"];
    }
    return _allArr;
}


-(NSArray *)digitalelArr {
    if (!_digitalelArr) {
        _digitalelArr = @[@"美食城",
                            @"商城商圈",
                            @"休闲娱乐",
                            @"KTV",
                            @"时尚丽人",
                            @"超市",
                            @"生活服务"];
    }
    return _digitalelArr;
}


/**
 *  MARK：XIB创建
 *
 *  @return
 */
+(RichAreaViewController*)CreatRichAreaViewController{
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    RichAreaViewController *richAreaViewController=[story instantiateViewControllerWithIdentifier:@"RichAreaViewController"];
    
    return richAreaViewController;
}

- (UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
        [self.view addSubview:_topView];
        self.lb1 = [[UILabel alloc] init];
        if (self.TuHao == 0) {
            self.lb1.text = @"0元专区";
        }
        if (self.TuHao == 0) {
            self.lb1.text = @"10币专区";
        }
        if (self.TuHao == 0) {
            self.lb1.text = @"百币专区";
        }
        if (self.TuHao == 0) {
            self.lb1.text = @"土豪专区";
        }
        self.lb1.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
        [_topView addSubview:self.lb1];
        [self.lb1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.view.frame.size.width/4 - 20);
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(40);
        }];
        UIImageView *iv1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"more_down"]];
        [_topView addSubview:iv1];
        [iv1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.lb1.mas_right).mas_equalTo(2);
            make.top.mas_equalTo(19);
            make.size.mas_equalTo(CGSizeMake(12, 12));
        }];
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_topView addSubview:btn1];
        btn1.backgroundColor = [UIColor clearColor];
        [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(self.view.frame.size.width / 2 - 1);
        }];
        
        UILabel *lb = [[UILabel alloc] init];
        [_topView addSubview:lb];
        lb.backgroundColor = [UIColor grayColor];
        [lb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(btn1.mas_right);
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(1);
        }];
        
        self.lb2 = [[UILabel alloc] init];
        self.lb2.text = @"全部";
        self.lb2.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
        [_topView addSubview:self.lb2];
        [self.lb2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(btn1.mas_right).mas_equalTo(self.view.frame.size.width/4 - 40);
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(80);
        }];
        UIImageView *iv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"more_down"]];
        iv1.contentMode = UIViewContentModeScaleAspectFit;
        iv2.contentMode = UIViewContentModeScaleAspectFit;
        [_topView addSubview:iv2];
        [iv2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.lb2.mas_right).mas_equalTo(2);
            make.top.mas_equalTo(19);
            make.size.mas_equalTo(CGSizeMake(12, 12));
        }];
        self.lb1.font = [UIFont systemFontOfSize:19];
        self.lb2.font = [UIFont systemFontOfSize:19];
        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_topView addSubview:btn2];
        btn2.backgroundColor = [UIColor clearColor];
        [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(self.view.frame.size.width / 2 - 1);
        }];
        
        [btn1 addTarget:self action:@selector(clickNearByBtn) forControlEvents:UIControlEventTouchUpInside];
        [btn2 addTarget:self action:@selector(clcikFoodSquareBtn) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor grayColor];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(50);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    return _topView;
    
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        [self.view addSubview:_tableView];
        [_tableView registerNib:[UINib nibWithNibName:@"StoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"StoreTableViewCell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(51);
            make.left.right.bottom.mas_equalTo(0);
        }];
    }
    return _tableView;
}


#pragma mark - tableView (tableViewDelegate/tableViewDataSource)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.tableView) {
        return 20;
    }else {
        return 6;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (tableView == self.tableView) {
        ChangePurchaeseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChangePurchaeseTableViewCell" forIndexPath:indexPath];
        cell.exchangeBtn.layer.cornerRadius = 14;
        cell.exchangeBtn.layer.masksToBounds = YES;
        cell.titleLb.numberOfLines = 0;
        return cell;
    }else if(tableView == self.allTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nearByCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"nearByCell"];
            if (indexPath.row < self.allArr.count) {
                cell.textLabel.text = self.allArr[indexPath.row];
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
                cell.backgroundColor = [UIColor whiteColor];
            }
        }
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nearByCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"nearByCell"];
            if (indexPath.row < self.digitalelArr.count) {
                cell.textLabel.text = self.digitalelArr[indexPath.row];
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
                cell.backgroundColor = [UIColor whiteColor];
            }
        }
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.tableView) {
        return 150;
    }else {
        return 44;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.blackView.frame = CGRectMake(0, 50, Width, 0);
    self.allTableView.frame = CGRectMake(0, 0, Width, 0);
    self.digitalTableView.frame = CGRectMake(0, 0, Width, 0);
    if (tableView == self.allTableView) {
        if (indexPath.row == 0) {
            self.lb1.text = @"全部";
        }
        if (indexPath.row == 1) {
            self.lb1.text = @"0元专区";
        }
        if (indexPath.row == 2) {
            self.lb1.text = @"10币专区";
        }
        if (indexPath.row == 3) {
            self.lb1.text = @"百币专区";
        }
        if (indexPath.row == 4) {
            self.lb1.text = @"土豪专区";
        }
    }else if (tableView == self.digitalTableView) {
        
    }else {
        
        GoodsDetailViewController *vc = [GoodsDetailViewController CreatGoodsDetailViewController];
        [self creatNav:vc];
        
    }
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [UIView animateWithDuration:0.5 animations:^{
        self.blackView.frame = CGRectMake(0, 50, Width, 0 );
        self.allTableView.frame = CGRectMake(0, 51, Width, 0);
        self.digitalTableView.frame = CGRectMake(0, 51, Width, 0);
    }];
}





//点击全部
- (void)clickNearByBtn {
    
    [UIView animateWithDuration:0.5 animations:^{
        self.blackView.frame = CGRectMake(0, 50, Width, Height - 50);
        self.allTableView.frame = CGRectMake(0, 50, Width, 220);
        self.digitalTableView.frame = CGRectMake(0, 50, Width, 0);
    }];
    
    
    
}
//点击手机数码
- (void)clcikFoodSquareBtn {
    
    [UIView animateWithDuration:0.5 animations:^{
        self.blackView.frame = CGRectMake(0, 50, Width, Height - 50);
        self.digitalTableView.frame = CGRectMake(0, 50, Width, 220);
        self.allTableView.frame = CGRectMake(0, 50, Width, 0);
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//创建模态导航控制器
-(void)creatNav:(UIViewController *)view{
    
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:view];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
    view.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
